#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        printf("argc is error\n");
        return 0;
    }

    // 定义文件描述符
    int fd = 0;
    //定义输入缓冲区
    char inbuffer[1024]= "\0";
    //定义输出缓冲区
    char outbuff[] = " hello char device\n";
    //调用open打开文件
    fd = open(argv[1], O_RDWR, 0666);
    if (fd < 0)
    {
        printf("open failed\n");
        return -1;
    }
    printf("open is ok \n");

    if (!strcmp(argv[2], "read"))
    {
        //读取文件内容到用户缓冲区
        read(fd, inbuffer, sizeof(inbuffer) - 1);
    }
    else if (!strcmp(argv[2], "write"))
    {
        //将用户缓冲区的内容写入
        write(fd, outbuff, sizeof(outbuff));
    }
    else 
    {
        //输入参数错误
        printf("please read or write\n");
    }
    //关闭文件
    close(fd);



    return 0;
}